<% content_for :sitemap do %>
  <span class="current">用户</span>
<% end %>

<%= form_tag admin_send_sms_path, method: :post do %>
  <div class="form-group">
    <%= text_area_tag "message", nil, placeholder: "说说看，为什么想加入？可以支持 Markdown 哦~", class: "topic-editor form-control" %>
  </div>
  <div class="form-group">
    <%= select_tag(:type, options_for_select([['个人','individual'], ['全部用户','all']], params[:type]), class: 'form-control') %>
  </div>
  <div class="form-group">
    <input type="text" name="login_name" class="form-control"  placeholder="用户名，逗号分割，比如：a,b,c" />
  </div>
  <button class="btn btn-primary">发送</button>
<% end %>

<hr>

<div class="toolbar">
  <form class="form-inline" action="<%= admin_users_path %>">
    <div class="form-group mr-sm-2">
      <%= select_tag(:field, options_for_select([['用户名','login'], ['Email','email'], ['姓名','name'], ['手机号','phone_number']], params[:field]), class: 'form-control') %>
    </div>

    <div class="form-group mr-sm-2">
      <input type="text" name="q" class="form-control" value="<%= params[:q] %>" placeholder="搜索" />
    </div>

    <div class="form-group mr-sm-2">
      <input type="text" name="last_sign_in_ip" class="form-control" value="<%= params[:last_sign_in_ip] %>" placeholder="last_sign_in_ip" />
    </div>

    <div class="form-group mr-sm-2">
      <%= select_tag(:type, options_for_select([['全部类别',''], ['社团','Team']], params[:type]), class: 'form-control') %>
    </div>
    <div class='d-flex'
          data-controller='roles-filter'
          data-roles-filter-selected-state-value='<%= params[:state] || ""%>'
          data-roles-filter-states-info-value='<%= user_states_info.to_json%>'
          data-roles-filter-state-blank-value='true'>
      <div class="form-group mr-sm-2">
        <%= select_tag(:category, options_for_select([['全部类别',''], ['个人账号','-1--99'], ['公众号','120--129'], ['企业号','130--139']], params[:category]), data: {"roles-filter-target" => "main", "action" => 'change->roles-filter#select'},  class: 'form-control') %>
      </div>

      <div class="form-group mr-sm-2" >
        <%= select_tag(:state,
        "",
        data: {"roles-filter-target" => "sub"},
        class: 'form-control',
        include_blank: "所有等级") %>
      </div>


    </div>
    <button class="btn btn-default">搜索</button>
  </form>


</div>

<div class="card-body item-list">
  <div class="form-group">
<%= link_to "ip 注册情况", ip_status_admin_users_path(), target: "_blank", class: "btn btn-primary"%>
<%= link_to "注册邀请码", admin_invite_codes_path(), target: "_blank", class: "btn btn-primary" %>
  </div>
</div>

<table class="table table-bordered table-striped table-condensed">
  <tr class="head">
    <td class="first">#</td>
    <td>头像</td>
    <td colspan="2">帐号</td>
    <td>权限</td>
    <td>注册时间</td>
    <td>上次登录ip</td>
    <td>当前登录ip</td>
    <td>积分</td>
    <td>操作</td>
  </tr>

  <% @users.each do |user| %>
    <tr>
      <td class="first"><%= user.id %></td>
      <td width="32"><%= user_avatar_tag(user, :sm) %></td>
      <% if user.maintainer? %>
        <td >
          <%= link_to user.fullname, user_path(user), target: "_blank" %> <%= user_level_tag(user) unless user.member? %>
        </td>
        <td>
          <%= Node.where(id: user.node_assignment_ids).pluck(:name).join(', ') %>
        </td>
      <% else %>
        <td colspan="2">
          <%= link_to user.fullname, user_path(user), target: "_blank" %> <%= user_level_tag(user) unless user.member? %>
        </td>
      <% end %>
      <td>
        <%= I18n.t("activerecord.enums.user.state.#{user.state}") %>
      </td>

      <td width="120"><%= user.created_at&.to_date %></td>
      <td width="120"><%= user.last_sign_in_ip %></td>
      <td width="120"><%= user.current_sign_in_ip %></td>
      <td >
        <%= link_to admin_credit_records_path(user_email_eq: user.email) do %>
        <%= user.credit_sum %>
        <%end %>
      </td>
      <td>
        <%= link_to '', edit_admin_user_path(user.id), class: "fa fa-edit mr-2", title: "edit user" %>
        <% if user.maintainer? %>
          <%= link_to edit_assign_nodes_admin_user_path(user.id), title: "assign nodes", 'data-disable-with': "loading...", remote: true do %>
            Assign
          <% end %>
        <% end %>
      </td>
    </tr>
  <% end %>
</table>
<%= paginate @users %>
